package com.cryptape.cita_wallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alm.token.R;
import com.cryptape.cita_wallet.bean.CloudMinerBean;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.List;

/**
 * Created by dengming on 23/2/20.
 */

public class CloudMinerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<CloudMinerBean> cloudMinerList;
    private CloudMinerAdapter.OnItemClickListener onItemClickListener;
    private Context mContext;

    public CloudMinerAdapter(Context context,List<CloudMinerBean> cloudMinerList){
        this.cloudMinerList = cloudMinerList;
        this.mContext = context;
    }

    public void setOnItemClickListener(CloudMinerAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ClountMinerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_clount_miner,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        ClountMinerViewHolder clountMinerViewHolder = ((ClountMinerViewHolder)holder);
        clountMinerViewHolder.btn_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(holder.itemView,(int) holder.itemView.getTag());
                }
            }
        });
        CloudMinerBean bean = cloudMinerList.get(position);
        if(bean.getType() == 0) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m1);
        }else if(bean.getType() == 1) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m2);
        }else if(bean.getType() == 2) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m3);
        }else if(bean.getType() == 3) {
            clountMinerViewHolder.miner_image.setBackgroundResource(R.drawable.icon_miner_m4);
        }
        clountMinerViewHolder.tv_miner_name.setText("M"+(bean.getType()+1)+ mContext.getString(R.string.cloud_miner));
        clountMinerViewHolder.tv_price.setText(bean.getPrice()+"HMIO");
        BigInteger power = new BigDecimal(bean.getPrice()).divide(new BigDecimal("500"), RoundingMode.DOWN).toBigInteger();
        clountMinerViewHolder.tv_power.setText(power.toString()+"T");
        clountMinerViewHolder.tv_left.setText(mContext.getString(R.string.Remain)+bean.getLeftAmount());
        if(new BigDecimal(bean.getLeftAmount()).compareTo(BigDecimal.ZERO) == 0){
            clountMinerViewHolder.btn_buy.setEnabled(false);
            clountMinerViewHolder.btn_buy.setBackgroundResource(R.drawable.button_corner_gray_shape);
        }else {
            clountMinerViewHolder.btn_buy.setEnabled(true);
            clountMinerViewHolder.btn_buy.setBackgroundResource(R.drawable.cd_btn_bg);
        }
//        ((ClountMinerViewHolder)holder).btn_buy.setTag(new Integer(position));
    }

    @Override
    public int getItemCount() {
        return cloudMinerList == null?0:cloudMinerList.size();
    }

    class ClountMinerViewHolder extends RecyclerView.ViewHolder {
        public Button btn_buy;
        ImageView miner_image;
        TextView tv_miner_name;
        TextView tv_power;
        TextView tv_price;
        TextView tv_left;

        public ClountMinerViewHolder(View view) {
            super(view);
//            view.setOnClickListener(v -> {
//                if (onItemClickListener != null) {
//                    onItemClickListener.onItemClick(v, (int) v.getTag());
//                }
//            });
            btn_buy = view.findViewById(R.id.btn_buy);
            miner_image = view.findViewById(R.id.miner_image);
            tv_miner_name  = view.findViewById(R.id.tv_miner_name);
            tv_power  = view.findViewById(R.id.tv_power);
            tv_price  = view.findViewById(R.id.tv_price);
            tv_left  = view.findViewById(R.id.tv_left);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
